//
//  VCOfSecondTopic.m
//  UI_09_homework_UiTableVIew_省市区
//
//  Created by li cheng on 16/7/26.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfSecondTopic.h"
#import "VCOfSecondZone.h"

@interface VCOfSecondTopic ()
@property (nonatomic, strong) NSMutableArray *provinces;
@property (nonatomic, strong) NSMutableArray *citys;
@property (nonatomic, strong) NSMutableArray *allCitys;
@property (nonatomic, strong) NSArray *lineArray;
@end

@implementation VCOfSecondTopic

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.citys = [NSMutableArray array];
    self.allCitys = [NSMutableArray array];
    NSString *sourceOfString = [NSString stringWithContentsOfFile:@"/Users/licheng/DLS10710/UI/HomeWork/UI_09_homework_UiTableVIew_省市区/UI_09_homework_UiTableVIew_省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
    self.lineArray = [sourceOfString componentsSeparatedByString:@"\n"];
    // 创建一个可变的数组,用来存储每个省
    self.provinces = [NSMutableArray array];
    // 遍历每一行字符串
    for (NSString *stringOfLine in self.lineArray) {
        // 判断是否为省, 创建省的字典
        if (![stringOfLine hasPrefix:@" "]) {
            // 创建可变的空得省字典
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            // 向省字典里中添加省名键值对
            [proDic setObject:stringOfLine forKey:@"proName"];
            // 创建一个市数组
            NSMutableArray *cityArray = [NSMutableArray array];
            // 将这个市数组添加到省字典中
            [proDic setObject:cityArray forKey:@"citys"];
            // 最后将这个省字典加入到省数组中
            [self.provinces addObject:proDic];
        }else if ([stringOfLine hasPrefix:@"  "] && ![stringOfLine hasPrefix:@"   "]){
            //创建一个可变的市字典
            NSMutableDictionary *cityDictionary = [NSMutableDictionary dictionary];
            //将市名添加到市字典中
            [cityDictionary setObject:stringOfLine forKey:@"cityName"];
            //创建一个区数组
            NSMutableArray *zoneArray = [NSMutableArray array];
            //再将区数组添加到市字典中，即为该市设置区
            [cityDictionary setObject:zoneArray forKey:@"zones"];
            //再将市字典添加到市数组中
            //1、首先要找到当前所在的省的字典，通过省数组来获得当前所在的省
            NSMutableDictionary *proDic = [self.provinces lastObject];
            //2、通过省字典的市数组的key值，将市字典添加到市数组中去，将该市添加到该省对应的市数组中去
            [[proDic objectForKey:@"citys"] addObject:cityDictionary];
        }else{
            //如果不是省，不是市，就是区
            //创建一个可变的区字典
            NSMutableDictionary *zoneDictionary = [NSMutableDictionary dictionary];
            //将区名添加到区字典中，设置区名
            [zoneDictionary setObject:stringOfLine forKey:@"zoneName"];
            //区下面没有数组了，不用再创建数组了，直接将区字典添加到对应的市的字典的区数组中去
            //1、先找到当前所在省的字典
            NSMutableDictionary *proDic = [self.provinces lastObject];
            //2、再找到省字典中的市数组中对应的市
            NSMutableDictionary *cityDic = [[proDic valueForKey:@"citys"] lastObject];
            //3、找到区所在市的区数组
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zones"];
            //4、再将区字典存放到对应的市数组中去
            [zoneArray addObject:zoneDictionary];
        }
    }
    NSLog(@"%@", self.provinces);
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _provinces.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [[_provinces objectAtIndex:section] objectForKey:@"citys"];
    return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    NSArray *array = [[self.provinces objectAtIndex:indexPath.section] objectForKey:@"citys"];
    cell.textLabel.text = [array[indexPath.row] objectForKey:@"cityName"];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_provinces[section] objectForKey:@"proName"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfSecondZone *vcOfSecondZone = [[VCOfSecondZone alloc] init];
    vcOfSecondZone.array =[[self.provinces[indexPath.section] objectForKey:@"citys"][indexPath.row] objectForKey:@"zones"];
    [self.navigationController pushViewController:vcOfSecondZone animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
